function handlePost(frame) {
	var xmlDoc;
	if (typeof frame.contentDocument != 'undefined') xmlDoc = frame.contentDocument.documentElement; else
	if (typeof frame.contentWindow.document.XMLDocument != 'undefined') xmlDoc = frame.contentWindow.document.XMLDocument.documentElement;
	else return;
    if (xmlDoc.getElementsByTagName('java.lang.Exception').length > 0) {
	    var errDiv = document.getElementById('ErrorPlaceHolder');
	    errDiv.innerHTML=xmlDoc.getElementsByTagName('detailMessage')[0].childNodes[0].nodeValue;
    	return;
    }
    if (xmlDoc.getElementsByTagName('string').length  == 1 && xmlDoc.childNodes.length <= 3) {
	    document.location=xmlDoc.getElementsByTagName('string')[0].childNodes[0].nodeValue;
    	return;
    }
    if (typeof ProcessXML != 'undefined') ProcessXML(xmlDoc);
}


function toggleAdvanced() {
	var adv=document.getElementById('AdvProp');
	if (adv.style.visibility=='hidden') {
		adv.style.visibility='visible';
		adv.style.position='fixed';
	} else {
		adv.style.visibility='hidden';
		adv.style.position='absolute';
	}
}
function toggle(postfix, b)
{
	var f = document.getElementById(postfix);
	var s = document.getElementById(postfix + '_Holder');
	if (b) {
		if (s.style.visibility=='visible') return;
		s.style.visibility='visible';
	} else {
		setTimeout("checkVisible('" + postfix + "')",10);
	}
}
function checktab(postfix, e)
{
	var keynum;
	if(window.event) // IE
	{
	keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
	keynum = e.which;
	}
	if (keynum == 9) {
		clearTimeout();
		var s = document.getElementById(postfix + '_Holder');
		s.style.visibility='hidden';
		document.getElementById(postfix).tabIndex = 1;
		document.getElementById(postfix).blur();
	}
}
function checkVisible(postfix)
{
	clearTimeout();
	var s = document.getElementById(postfix + '_Holder');
	var active = document.activeElement.id;
	if (active == 'Country' + postfix || active == 'Region' + postfix || active == 'City' + postfix || active == postfix + '_Holder' || active == postfix) {
		setTimeout("checkVisible('" + postfix + "')", 50);
		return;
	}
	s.style.visibility='hidden';

}

function onChangeCountry(postfix, userinit) {
	var c = document.getElementById('Country' + postfix);
	var op = c.options[c.selectedIndex];
	var f = document.getElementById(postfix);
	if (userinit != 0) f.focus();
	if (op.value!='0') f.value = op.text; else f.value = '';
	var rg = document.getElementById('reg'+postfix);
	if (rg != null) {
		rg.value=op.value;
	}
}
function onChangeRegion(postfix, userinit) {
	var c = document.getElementById('Country'+postfix);
	var r = document.getElementById('Region'+postfix);
	var op = c.options[c.selectedIndex];
	var op1 = r.options[r.selectedIndex];
	var f = document.getElementById(postfix);
	if (userinit != 0) f.focus();
	if (op1.value > 0) {
		f.value = op1.text + ', ' + op.text;
	} else f.value = op.text;

	var rg = document.getElementById('reg'+postfix);
	if (rg != null) {
		rg.value=op.value + '-' + op1.value;
	}
}
function onChangeCity(postfix, userinit) {
	var c = document.getElementById('Country'+postfix);
	var r = document.getElementById('Region'+postfix);
	var ct = document.getElementById('City'+postfix);
	var op = c.options[c.selectedIndex];
	if (r.selectedIndex >= 0)
		var op1 = r.options[r.selectedIndex];
	else op1 = r.options[0];
	var op2 = ct.options[ct.selectedIndex];
	var f = document.getElementById(postfix);
	if (userinit != 0) f.focus();
	var s = '';
	if (op1.value > 0) {
		s = op1.text + ', ' + op.text;
	} else s = op.text;

	if (ct.selectedIndex == 0) f.value=s; else
	f.value = ct.options[ct.selectedIndex].text + ', ' + s;
	var rg = document.getElementById('reg'+postfix);
	if (rg != null) {
		rg.value=op.value + '-' + op1.value + '-' + op2.value;
	}
}
function change(postfix) {
	var f = document.getElementById(postfix);
    if ((oldReqStr[postfix] || oldReqStr[postfix] != f.value) && f.value.length > 0) {
        var url = "matchcity?name=" + encodeURI(f.value) + "&val="+postfix;
        SendRequest(url, foundCity);
        oldReqStr[postfix] = f.value;
    }
}

function foundCity(xmlDom) {
	var e = xmlDom.getElementsByTagName('boolean');
	if (e.length > 0) return;
	var postfix = xmlDoc.firstChild.attributes.getNamedItem('value').firstChild.nodeValue;
	var cid = xmlDom.getElementsByTagName('CountryID')[0].childNodes[0].nodeValue;
	var rid = xmlDom.getElementsByTagName('RegionID')[0].childNodes[0].nodeValue;
	var id = xmlDom.getElementsByTagName('CityID')[0].childNodes[0].nodeValue;
	selectRegionElement(postfix, cid, rid, id);
}

function gouser()
{
    window.location='user.jsp';
}
function changeTransport(sel)
{
    var url = 'gettransport?tid=' + sel.value;
    if (sel.value!='')
    	SendRequest(url, updateTransport);
    else clearTransport();
}
function updateTransport(xmlDom) {
	var e = xmlDom.getElementsByTagName('boolean');
	if (e.length > 0) return;
	var number = '';
	if (xmlDom.getElementsByTagName('number')[0].childNodes.length > 0) 
		number = xmlDom.getElementsByTagName('number')[0].childNodes[0].nodeValue;
	var type = xmlDom.getElementsByTagName('type')[0].childNodes[0].nodeValue;
	var height = xmlDom.getElementsByTagName('height')[0].childNodes[0].nodeValue;
	var width = xmlDom.getElementsByTagName('width')[0].childNodes[0].nodeValue;
	var length = xmlDom.getElementsByTagName('length')[0].childNodes[0].nodeValue;
	var tir = xmlDom.getElementsByTagName('tir')[0].childNodes[0].nodeValue;
	var ekmt = xmlDom.getElementsByTagName('ekmt')[0].childNodes[0].nodeValue;
	var adr = xmlDom.getElementsByTagName('adr')[0].childNodes[0].nodeValue;
	var tonnage = xmlDom.getElementsByTagName('tonnage')[0].childNodes[0].nodeValue;
	var volume = xmlDom.getElementsByTagName('volume')[0].childNodes[0].nodeValue;

	var sel = document.getElementById('cargo_type');
	sel.value = type;
	sel.readonly = true;
	sel = document.getElementById('volume');
	sel.value = volume;
	sel = document.getElementById('tonnage');
	sel.value = tonnage;
	sel = document.getElementById('number');
	sel.value = number;
	sel.readonly = true;
	sel = document.getElementById('adr');
	if (adr > 0) {
		sel.value = adr;
	} else {
		sel.value = '';
	}
	sel.readonly = true;
	sel = document.getElementById('tir');
	if (tir=='true') {
		sel.checked = true;
	} else {
		sel.checked = false;
	}
	sel.readonly = true;
	sel = document.getElementById('ekmt');
	if (ekmt=='true') {
		sel.checked = true;
	} else {
		sel.checked = false;
	}
	sel.readonly = true;
	sel = document.getElementById('width');
	if (width != '0.0') {
		sel.value = width;
	} else {
		sel.value = '';
	}
	sel.readonly = true;
	sel = document.getElementById('height');
	if (width != '0.0') {
		sel.value = height;
	} else {
		sel.value = '';
	}
	sel.readonly = true;
	sel = document.getElementById('length');
	if (width != '0.0') {
		sel.value = length;
	} else {
		sel.value = '';
	}
	sel.readonly = true;
	var loading = xmlDom.getElementsByTagName('loading')[0].childNodes;
	for (var i = 1; i <= maxLds; i++) {
		sel = document.getElementById('load' + i);
		if (sel) {
			sel.checked = false;
			sel.readonly = true;
		}
	}
	for (i = 0; i < loading.length; i++) {
		var l = loading[i].childNodes[0].nodeValue;
		sel = document.getElementById('load' + l);
		sel.checked = true;
	}

}
function clearTransport(xmlDom) {
	var sel = document.getElementById('cargo_type');
	sel.value = '';
	sel.readonly = false;
	sel = document.getElementById('volume');
	sel.value = '';
	sel = document.getElementById('tonnage');
	sel.value = '';
	sel = document.getElementById('number');
	sel.value = '';
	sel.readonly = false;
	sel = document.getElementById('adr');
	sel.value = '';
	sel.readonly = false;
	sel = document.getElementById('tir');
	sel.checked = false;
	sel.readonly = false;
	sel = document.getElementById('ekmt');
	sel.checked = false;
	sel.readonly = false;
	sel = document.getElementById('width');
	sel.value = '';
	sel.readonly = false;
	sel = document.getElementById('height');
	sel.value = '';
	sel.readonly = false;
	sel = document.getElementById('length');
	sel.value = '';
	sel.readonly = false;
	for (var i = 1; i <= maxLds; i++) {
		sel = document.getElementById('load' + i);
		if (sel) {
			sel.checked = false;
			sel.readonly = false;
		}
	}
}
function initTransport()
{
	var sel = document.getElementById('own_transport');
	if (sel != null) {
		if (sel.value != '') {
			changeTransport(sel);
		}
	}	
}
var oldReqStr = new Array();
var hashID='';
var start_from = 0;

function transformIE(n, xml)
{
    if (xslDoc[n].childNodes == null || xslDoc[n].childNodes.length == 0) xslDoc[n].load(xslDocNames[n]);
    var newFragment = xml.transformNode (xslDoc[n]);
    var area = document.getElementById('MsgArea');
    area.innerHTML = newFragment;

}
function transformMozilla(n, xml)
{
    var domTransform = document.implementation.createDocument("", "transform", null);
    if (xslDoc[n] == null) {
        var myXMLHTTPRequest = new XMLHttpRequest();
        myXMLHTTPRequest.open("GET", xslDocNames[n], false);
        myXMLHTTPRequest.send(null);
        xslDoc[n] = myXMLHTTPRequest.responseXML;
    }
    var processor = new XSLTProcessor();
    processor.importStylesheet(xslDoc[n]);
    var newFragment = processor.transformToFragment(xml, domTransform);
    var area = document.getElementById('MsgArea');
    while (area.childNodes.length > 0) area.removeChild(area.childNodes[0]);
    area.appendChild(newFragment);

}

function ProcessSearchXML(xmlDoc) {
	var result = '';
	document.getElementById('btn1').disabled=false;
	if (xmlDoc.getElementsByTagName('com.transcargo.posts.Freight-array').length > 0) {
		if (typeof XSLTProcessor != "undefined") {
            transformMozilla(0, xmlDoc);
        } else if (typeof xmlDoc.transformNode != "undefined") {
            transformIE(0, xmlDoc);
        }
	} else
		if (xmlDoc.getElementsByTagName('com.transcargo.posts.Cargo-array').length > 0) {
            if (typeof XSLTProcessor != "undefined") {
                transformMozilla(0, xmlDoc);
            } else if (typeof xmlDoc.transformNode != "undefined") {
                transformIE(0, xmlDoc);
            }
		} else
			if (xmlDoc.getElementsByTagName('boolean').length > 0) {
				if (xmlDoc.getElementsByTagName('boolean')[0].childNodes[0].nodeValue == 'false') {
		            if (typeof XSLTProcessor != "undefined") {
		                transformMozilla(1, xmlDoc);
		            } else if (typeof xmlDoc.transformNode != "undefined") {
		                transformIE(1, xmlDoc);
		            }
				}
			}
	if (xmlDoc.firstChild.attributes.length>0) {
		hashID = xmlDoc.firstChild.attributes.getNamedItem('hash').firstChild.nodeValue;
	    setTimeout('updateSearch(\''+hashID+'\')', 60000);
        document.cookie = 'searchHash='+hashID+'; expires=Wed, 1 Jan 2020 00:00:00 UTC; path=/';
    }
}
function showRotation() {
	var ma = document.getElementById('MsgArea');
	if (ma.innerHTML.length == 0) {
		ma.innerHTML = '<center><img src="img/bigrotation2.gif"></center>';
	} else {
		var s = ma.innerHTML;
		var h = ma.offsetHeight/2 + 16;
		var w = ma.offsetWidth/2 - 16;
		if (ma.style.opacity=='') {
			ma.innerHTML = '<div style="opacity:0.3;filter=alpha(opacity=0.3);zoom: 1;">' + s + '</div><img src="img/bigrotation2.gif" align="left" style="position:relative;left:'+w+'px;top:-'+h+'px;">';
		} else {
			ma.innerHTML = '<div style="opacity:30;filter=alpha(opacity=30);zoom: 1;">' + s + '</div><img src="img/bigrotation2.gif" align="left" style="position:relative;left:'+w+'px;top:-'+h+'px;">';
		}
	}
}

function submitSearch()
{
	var form = document.forms['sform'];
	var e = '?';
	for (var i = 0; i < form.elements.length; i++) {
		var el = form.elements[i];
		if (el.type == 'text' || el.type == 'select-one' || el.type == 'hidden')
			e = e + el.name + '=' + encodeURI(el.value) + '&';
		else if (el.type=='radio' || el.type=='checkbox') {
			if (el.checked == true) e = e + el.name + '=' + encodeURI(el.value) + '&';
		}
	}
	showRotation();
	document.getElementById('btn1').disabled=true;
	SendRequest('SearchRequest' + e, ProcessSearchXML);
}
function submitSearch(what)
{
	var form = document.forms['sform'];
	var e = '?';
	for (var i = 0; i < form.elements.length; i++) {
		var el = form.elements[i];
		if (el.type == 'text' || el.type == 'select-one' || el.type == 'hidden')
			e = e + el.name + '=' + encodeURI(el.value) + '&';
		else if (el.type=='radio' || el.type=='checkbox') {
			if (el.checked == true) e = e + el.name + '=' + encodeURI(el.value) + '&';
		}
	}
	if (what != null) e = e + '&what=' + what;
	showRotation();
	document.getElementById('btn1').disabled=true;
	SendRequest('SearchRequest' + e, ProcessSearchXML);
}

function customSearch(newSearch)
{
	showRotation();
	document.getElementById('btn1').disabled=true;
	SendRequest('SearchRequest?' + newSearch, ProcessSearchXML);
}
function updateSearch(hash) {
	SendRequest('ContinueSearchRequest?hash=' + hashID + '&sfrom=' + start_from, ProcessSearchXML);
}
function sfrom(page) {
	clearTimeout();
	start_from = page;
	showRotation();
	document.getElementById('btn1').disabled=true;
    SendRequest('ContinueSearchRequest?hash=' + hashID + '&sfrom=' + page + '&override', ProcessSearchXML);
}
function changeUser()
{
    var url = 'getusercombo?uid=' + document.forms['userform'].id.value + '&name='+ encodeURI(document.forms['userform'].name.value);
    if (document.forms['userform'].id.value!='' || document.forms['userform'].name.value != '')
    	SendRequest(url, updateUser);
	showRotation();
}
function getUser(id)
{
    var url = 'getusercombo?uid=' + id;
   	SendRequest(url, updateUser);
	showRotation();
}

function updateUser(xmlDoc) {
	var result = '';
	if (xmlDoc.getElementsByTagName('com.transcargo.users.LongUser').length > 0 || xmlDoc.getElementsByTagName('com.transcargo.users.ShortUser').length > 0) {
		if (typeof XSLTProcessor != "undefined") {
            transformMozilla(0, xmlDoc);
        } else if (typeof xmlDoc.transformNode != "undefined") {
            transformIE(0, xmlDoc);
        }
	} else {
		if (typeof XSLTProcessor != "undefined") {
            transformMozilla(1, xmlDoc);
        } else if (typeof xmlDoc.transformNode != "undefined") {
            transformIE(1, xmlDoc);
        }
	}
}

function ProcessRecipient(xmlDoc)
{
    if (xmlDoc.getElementsByTagName('com.transcargo.users.ShortUser').length > 0) {
        var id = xmlDoc.getElementsByTagName('Id')[0].childNodes[0].nodeValue;
        var name = xmlDoc.getElementsByTagName('name')[0].childNodes[0].nodeValue;
        document.forms['mailform'].to.value = name;
        document.forms['mailform'].recipient.value = id;
    }
}

//	checking mail box
function checkMail()
{
    var url = 'getmailheaders';
    if (inbox=='inbox' ) url = url + '?inbox=on'; 
    SendRequest(url, updateMail);
	showRotation();
}

// receiving data from the box
function updateMail(xmlDoc) {
	var result = '';
	if (xmlDoc.getElementsByTagName('com.transcargo.mail.MailHeader-array').length > 0) {
		if (typeof XSLTProcessor != "undefined") {
            transformMozilla(0, xmlDoc);
        } else if (typeof xmlDoc.transformNode != "undefined") {
            transformIE(0, xmlDoc);
        }
	} else document.getElementById('MsgArea').innerHTML = '';
}
//getting single letter
function getMail(id)
{
    var url = 'getmail?id=' + id;
    SendRequest(url, updateReadMail);
	showRotation();
}

// receiving letter's data
function updateReadMail(xmlDoc) {
	var result = '';
	if (xmlDoc.getElementsByTagName('com.transcargo.mail.Mail').length > 0) {
		if (typeof XSLTProcessor != "undefined") {
            transformMozilla(0, xmlDoc);
        } else if (typeof xmlDoc.transformNode != "undefined") {
            transformIE(0, xmlDoc);
        }
	} else document.getElementById('MsgArea').innerHTML = '';
}

// deleting mail
function delmailProcess() {
    var url = 'DelMailAction';
    var data = '';
	var form = document.forms['delmail'];
	for (var i = 0; i < form.elements.length; i++) {
		var el = form.elements[i];
		if (el.type=='checkbox') {
			if (el.checked == true) {
				if (data != '') data = data + '&';
				data = data + el.name + '=on';
			}
		}
	}
    SendAction(url, data, updateDelMail);
	showRotation();
}

//	getting data from deleting
function updateDelMail(xmlDoc) {
    var url = 'getmailheaders';
    if (inbox=='inbox' ) url = url + '?inbox=on'; 
    SendRequest(url, updateMail);
}

function submitDistance()
{
	var form = document.forms['sform'];
	var e = '?';
	for (var i = 0; i < form.elements.length; i++) {
		var el = form.elements[i];
		if (el.type == 'text' || el.type == 'select-one' || el.type == 'hidden')
			e = e + el.name + '=' + encodeURI(el.value) + '&';
		else if (el.type=='radio' || el.type=='checkbox') {
			if (el.checked == true) e = e + el.name + '=' + encodeURI(el.value) + '&';
		}
	}
	showRotation();
	SendRequest('GetDistanceRequest' + e, ProcessDistanceXML);
}

function ProcessDistanceXML(xmlDoc) {
	var result = '';
	if (xmlDoc.getElementsByTagName('com.transcargo.distance.Distance').length > 0) {
		if (typeof XSLTProcessor != "undefined") {
            transformMozilla(0, xmlDoc);
        } else if (typeof xmlDoc.transformNode != "undefined") {
            transformIE(0, xmlDoc);
        }
	} else
		if (xmlDoc.getElementsByTagName('boolean').length > 0) {
			if (xmlDoc.getElementsByTagName('boolean')[0].childNodes[0].nodeValue == 'false') {
	            if (typeof XSLTProcessor != "undefined") {
	                transformMozilla(1, xmlDoc);
	            } else if (typeof xmlDoc.transformNode != "undefined") {
	                transformIE(1, xmlDoc);
	            }
			}
		}
}